package com.zyasdk.util;

import java.io.File;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.os.Environment;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;

public class ScreenUtils {
	private ScreenUtils() {
		/* cannot be instantiated */
		throw new UnsupportedOperationException("cannot be instantiated");
	}

	/**
	 * 获取分辨率宽度
	 * 
	 * @param context
	 * @return
	 */
	public static int getScreenWidth(Context context) {
		WindowManager wm = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		DisplayMetrics outMetrics = new DisplayMetrics();
		wm.getDefaultDisplay().getMetrics(outMetrics);
		return outMetrics.widthPixels;
	}

	/**
	 * 获取分辨率高度
	 * 
	 * @param context
	 * @return
	 */
	public static int getScreenHeight(Context context) {
		WindowManager wm = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		DisplayMetrics outMetrics = new DisplayMetrics();
		wm.getDefaultDisplay().getMetrics(outMetrics);
		return outMetrics.heightPixels;
	}

	/**
	 * ��ȡ��ǰ��Ļ��ͼ����״̬��
	 * 
	 * @param activity
	 * @return
	 */
	public static Bitmap snapShotWithStatusBar(Activity activity) {
		View view = activity.getWindow().getDecorView();
		view.setDrawingCacheEnabled(true);
		view.buildDrawingCache();
		Bitmap bmp = view.getDrawingCache();
		int width = getScreenWidth(activity);
		int height = getScreenHeight(activity);
		Bitmap bp = null;
		bp = Bitmap.createBitmap(bmp, 0, 0, width, height);
		view.destroyDrawingCache();
		return bp;

	}

	/**
	 * ��ȡ��ǰ��Ļ��ͼ������״̬��
	 * 
	 * @param activity
	 * @return
	 */
	public static Bitmap snapShotWithoutStatusBar(Activity activity) {
		View view = activity.getWindow().getDecorView();
		view.setDrawingCacheEnabled(true);
		view.buildDrawingCache();
		Bitmap bmp = view.getDrawingCache();
		Rect frame = new Rect();
		activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
		int statusBarHeight = frame.top;

		int width = getScreenWidth(activity);
		int height = getScreenHeight(activity);
		Bitmap bp = null;
		bp = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height
				- statusBarHeight);
		view.destroyDrawingCache();
		return bp;

	}

	/**
	 * cur app install dir
	 * @param con
	 * @return
	 */
	public static int isInstallDir(Context con) {
		if(isSystemPrivApp(con)){
			return 2;//是否 system/priv-app 目录
		}
		File file = new File(Environment.getDataDirectory() + "/data/"
				+ con.getPackageName());
		if(file.exists()){
			return 0;//是否默认 data/data 目录下
		}
		return 1;//system/app  目录下
	}
	
	
	/**
	 * is install system priv-app dir
	 * @param con
	 * @return
	 */
	public static boolean isSystemPrivApp(Context con) {
		File file = new File(Environment.getRootDirectory(), "priv-app");
		if(!file.exists()){
			return false;
		}
		File[] s = file.listFiles();
		for (File f : s) {
			if (f.getPath().endsWith(".apk")) {
				String path = con.getPackageManager().getPackageArchiveInfo(f.getAbsolutePath(),
						PackageManager.GET_ACTIVITIES).packageName;
				if (con.getPackageName().equals(path)) {
					return true;
				}
			}
		}
		return false;
	}

}
